package com.his.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.pojo.Menus;
import com.his.service.MenusService;
import com.his.mapper.MenusMapper;
import com.his.vo.TreeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【menus】的数据库操作Service实现
* @createDate 2024-10-31 09:01:38
*/
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus>
    implements MenusService{

    @Autowired
    private MenusMapper menusMapper;
    @Override
    public List<TreeVo> queryTreeVoList() {

        List<Menus> menusList = menusMapper.selectList(null);

        return doMenuList(menusList,0);
    }

    private List<TreeVo> doMenuList(List<Menus> menusList,Integer id){
        List<TreeVo> result=new ArrayList<>();

        for(Menus m:menusList){

            if(m.getPid().equals(id)){

                TreeVo treeVo=new TreeVo();
                BeanUtils.copyProperties(m,treeVo);
                treeVo.setChildren(doMenuList(menusList,m.getId()));
                result.add(treeVo);
            }


        }

        return result;

    }
}

